AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
local flaretime = 0
local ClassNamesA = {
	"prop_vehicle_jeep",
	"prop_vehicle_airboat", 
	"prop_vehicle_pod", 
	"gmod_hoverball",
	"gmod_wheel",
	"gmod_thruster",
	"energy_cell",
	"res_cache"
	}
local ClassNamesB = {
	"air_scrubber",
	"air_exchanger",
	"heat_exchanger",
	"climate_control",
	"env_probe",
    "air_compressor",
	"air_scrubber",
	"water_destabilizer",
	"hydrogen_processor",
	"coolant_compressor",
	"coolant_electrolyzer",
	"coolant_delectrolyzer",
	"energy_wind",
	"energy_hydro",
	"warp_core",
	"microfusion_core",
	"uni_res_gen",
	"fission_core",
	"uranium_miner"
				}
	
local disabledDevs = {}

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create("sent_spaceanon_solar_flare")
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Initialize()
	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(false)
		phys:EnableCollisions(true)
	end
	self.Entity:SetColor(0,0,0,255)
	self.Current = Vector(math.random(0,1),math.random(0,1),math.random(0,1))
	self.Power = math.random(512,1024)+1024
	--FLARE XD
	range = ents.FindInSphere(self.Entity:GetPos(),99999999)
	Msg("Time: "..math.Round(self.Power/64).."\n")
	for k,v in pairs (range) do
		if v:IsValid() then
			local gotit = false
			for _,name in pairs(ClassNamesA) do
				if v:GetClass() == name then
					Flare(v,math.Round(self.Power/64))
					gotit = true
					break
				end
			end
			if not gotit then
				for _,name in pairs(ClassNamesB) do
					if v:GetClass() == name then
						table.insert(disabledDevs, v)
						flaretime = math.Round(self.Power/64)+CurTime()
						break
					end
				end
			end
		end
	end
	self.Entity:Remove() --awww
	local plys = player.GetAll()
	for k,v in pairs(plys) do
		v:SendLua("Flareflash()")
	end
end

function CheckDevs()
	for k,v in pairs(disabledDevs) do
		if v:IsValid() then
			if flaretime < CurTime() then
				table.remove(disabledDevs, k)
			else
				v:TriggerInput("On", 0)
			end
		else
			table.remove(disabledDevs, k)
		end
	end	
end
hook.Add("Think", "DevsOffFlare", CheckDevs)

function Flare(ent,resettime)
	if ent:GetClass() == "prop_vehicle_jeep" or ent:GetClass() == "prop_vehicle_airboat" then
		ent:Fire("TurnOff","",0)
		ent:Fire("TurnOn","",resettime)
		ent:Fire("HandBrakeOn","",0)
		ent:Fire("HandBrakeOff","",resettime)
	end
	if ent:GetClass() == "prop_vehicle_prisoner_pod" then
		ent:Fire("Lock","",0)
		ent:Fire("Unlock","",resettime)
	end
	if ent:GetClass() == "gmod_wheel" then
		local torq = ent.BaseTorque
		local torq2 = ent.BaseTorque * ent.TorqueScale
		timer.Simple(resettime,RTTorq,ent,torq,torq2)
		ent:SetBaseTorque( 1 )
		ent:SetTorque( 0 )
	elseif ent:GetClass() == "gmod_thruster" then
		local power = ent.force
		timer.Simple(resettime,RTForce,ent,power)
		ent:SetForce(0)
	elseif ent:GetClass() == "gmod_hoverball" then
		local speed = ent:GetSpeed()
		ent:SetSpeed(0)
		timer.Simple(resettime,RTSpeed,ent,speed)
	elseif ((ent:GetClass() == "energy_cell") or (ent:GetClass() == "res_cache"))then
		entConsumeResource("energy", ent:GetResourceAmount("energy")/2)
	end
end


function RTForce(ent,power)
	if !ent:IsValid() then return end
	ent:SetForce( force )
end
function RTSpeed(ent,speed)
	if !ent:IsValid() then return end
	ent:SetSpeed( speed )
end
function RTTorq(ent,torq,torq2)
	if !ent:IsValid() then return end
	ent:SetBaseTorque( torq )
	ent:SetTorque( torq2 )
end